{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8432b47e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import os\n",
    "from os import listdir\n",
    "from sklearn.model_selection import KFold,StratifiedKFold\n",
    "import random\n",
    "from sklearn.datasets import load_svmlight_file\n",
    "# 加载数据集\n",
    "def loadfile(filename):\n",
    "    X_train, y_train = load_svmlight_file(filename)\n",
    "    x = X_train.todense()\n",
    "    y_train = y_train.astype(np.int)\n",
    "    return np.array(x), np.array(y_train)\n",
    "def savelibsvm(x, y, filename):\n",
    "    f =open(filename, \"w\", encoding='UTF-8')\n",
    "    m, n = x.shape\n",
    "    for index in range(m):\n",
    "        out = str(y[index])\n",
    "        for i in range(n):\n",
    "            out = out + \"\\t{}:{}\".format(i+1,x[index,i])\n",
    "        out = out + \"\\n\"\n",
    "        f.writelines(out)\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "614417e8",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abalone-17_vs_7-8-9-10_无性别完成！\n",
      "abalone-17_vs_7-8-9-10_有性别完成！\n",
      "abalone19_无性别完成！\n",
      "abalone19_有性别完成！\n",
      "ecoli-0_vs_1_libsvm完成！\n",
      "ecoli1_libsvm完成！\n",
      "ecoli2_libsvm完成！\n",
      "ecoli3_libsvm完成！\n",
      "ecoli4_libsvm完成！\n",
      "glass0_libsvm完成！\n",
      "haberman_libsvm完成！\n",
      "iris0_libsvm完成！\n",
      "page-blocks0_libsvm完成！\n",
      "pima_libsvm完成！\n",
      "winequality-red-8_vs_6_libsvm完成！\n",
      "yeast-0-5-6-7-9_vs_4_libsvm完成！\n",
      "yeast-1_vs_7_libsvm完成！\n",
      "yeast-2_vs_4_libsvm完成！\n",
      "yeast3_libsvm完成！\n",
      "yeast4_libsvm完成！\n",
      "abalone-17_vs_7-8-9-10_无性别完成！\n",
      "abalone-17_vs_7-8-9-10_有性别完成！\n",
      "abalone19_无性别完成！\n",
      "abalone19_有性别完成！\n",
      "ecoli-0_vs_1_libsvm完成！\n",
      "ecoli1_libsvm完成！\n",
      "ecoli2_libsvm完成！\n",
      "ecoli3_libsvm完成！\n",
      "ecoli4_libsvm完成！\n",
      "glass0_libsvm完成！\n",
      "haberman_libsvm完成！\n",
      "iris0_libsvm完成！\n",
      "page-blocks0_libsvm完成！\n",
      "pima_libsvm完成！\n",
      "winequality-red-8_vs_6_libsvm完成！\n",
      "yeast-0-5-6-7-9_vs_4_libsvm完成！\n",
      "yeast-1_vs_7_libsvm完成！\n",
      "yeast-2_vs_4_libsvm完成！\n",
      "yeast3_libsvm完成！\n",
      "yeast4_libsvm完成！\n",
      "abalone-17_vs_7-8-9-10_无性别完成！\n",
      "abalone-17_vs_7-8-9-10_有性别完成！\n",
      "abalone19_无性别完成！\n",
      "abalone19_有性别完成！\n",
      "ecoli-0_vs_1_libsvm完成！\n",
      "ecoli1_libsvm完成！\n",
      "ecoli2_libsvm完成！\n",
      "ecoli3_libsvm完成！\n",
      "ecoli4_libsvm完成！\n",
      "glass0_libsvm完成！\n",
      "haberman_libsvm完成！\n",
      "iris0_libsvm完成！\n",
      "page-blocks0_libsvm完成！\n",
      "pima_libsvm完成！\n",
      "winequality-red-8_vs_6_libsvm完成！\n",
      "yeast-0-5-6-7-9_vs_4_libsvm完成！\n",
      "yeast-1_vs_7_libsvm完成！\n",
      "yeast-2_vs_4_libsvm完成！\n",
      "yeast3_libsvm完成！\n",
      "yeast4_libsvm完成！\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "#表示k折的次数\n",
    "KFold_num = 3\n",
    "# 放入数据的文件夹\n",
    "data_dir_all = '0221-data/robust'\n",
    "\n",
    "# 交叉验证产生的数据集存放的文件夹\n",
    "data_after = '0221-after-robust2'\n",
    "# 表示几折交叉验证\n",
    "n_k = 3\n",
    "dircontent = listdir(data_dir_all)\n",
    "for k_num in range(KFold_num):\n",
    "    # 创建放入数据的文件夹\n",
    "    try:\n",
    "        os.mkdir(data_after)\n",
    "    except OSError as error:\n",
    "        pass\n",
    "    # 每个k折的数据集存放目录\n",
    "    save_dir_ls = \"{}/{}\".format(data_after,k_num)\n",
    "    try:\n",
    "        os.mkdir(save_dir_ls)\n",
    "    except OSError as error:\n",
    "        pass\n",
    "    \n",
    "    for dir_file in dircontent:\n",
    "        dir_new = dir_file.split(\".\")[0]\n",
    "        try:\n",
    "            os.mkdir('{}/{}'.format(save_dir_ls, dir_new))\n",
    "        except OSError as error:\n",
    "            pass\n",
    "        x, y = loadfile(\"{}/{}\".format(data_dir_all, dir_file))\n",
    "        skf = StratifiedKFold(n_splits=n_k, shuffle=True, random_state=random.randint(1000,10000))\n",
    "        i = 0\n",
    "        for train_index, test_index in skf.split(x, y):\n",
    "            i = i + 1\n",
    "            X_train, X_test = x[train_index], x[test_index]\n",
    "\n",
    "            y_train, y_test = y[train_index], y[test_index] \n",
    "            savelibsvm(X_train, y_train, \"{}/{}/{}train.txt\".format(save_dir_ls, dir_new, i))\n",
    "            savelibsvm(X_test, y_test, \"{}/{}/{}test.txt\".format(save_dir_ls, dir_new, i))\n",
    "        print(\"{}完成！\".format(dir_new))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d24211cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "grain_vs_cotton_kpca_cosine_100完成！\n",
      "grain_vs_cotton_kpca_rbf_100完成！\n",
      "grain_vs_cotton_mds_100完成！\n"
     ]
    }
   ],
   "source": [
    "# 放入数据的文件夹\n",
    "data_dir_all = 'test'\n",
    "dircontent = listdir(data_dir_all)\n",
    "# 交叉验证产生的数据集存放的文件夹\n",
    "data_after = 'test_after3'\n",
    "# 表示几折交叉验证\n",
    "n_k = 3\n",
    "\n",
    "try:\n",
    "    os.mkdir(data_after)\n",
    "except OSError as error:\n",
    "    pass\n",
    "for dir in dircontent:\n",
    "    dir_new = dir.split(\".\")[0]\n",
    "    try:\n",
    "        os.mkdir('{}/{}'.format(data_after, dir_new))\n",
    "    except OSError as error:\n",
    "        pass\n",
    "    x, y = loadfile(\"{}/{}\".format(data_dir_all, dir))\n",
    "    skf = StratifiedKFold(n_splits=n_k, shuffle=True, random_state=random.randint(1000,10000))\n",
    "    i = 0\n",
    "    for train_index, test_index in skf.split(x, y):\n",
    "        i = i + 1\n",
    "        X_train, X_test = x[train_index], x[test_index]\n",
    "\n",
    "        y_train, y_test = y[train_index], y[test_index] \n",
    "        savelibsvm(X_train, y_train, \"{}/{}/{}train.txt\".format(data_after, dir_new, i))\n",
    "        savelibsvm(X_test, y_test, \"{}/{}/{}test.txt\".format(data_after, dir_new, i))\n",
    "    print(\"{}完成！\".format(dir_new))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d397ca0e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py36",
   "language": "python",
   "name": "py36"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
